﻿using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Security.Cryptography;

namespace Utility
{
    /// <summary>
    /// 鍏辩敤缁勪欢
    /// </summary>
    public abstract class Common
    {
        /// <summary>
        /// Check object isn't null and empty.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool CheckValiable(object obj)
        {
            if (Object.Equals(obj, null) || Object.Equals(obj, string.Empty))
                return false;
            else
                return true;
        }

        /// <summary>
        /// 杩斿洖32浣?MD5 鍔犲瘑鍊?
        /// </summary>
        /// <param name="strMsg"></param>
        /// <returns></returns>
        public static string MD5(string strMsg)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(strMsg ?? "", "MD5");
        }
        public static string Hash(string toHash)
        {
            MD5CryptoServiceProvider crypto = new MD5CryptoServiceProvider();
            byte[] bytes = Encoding.UTF7.GetBytes(toHash);
            bytes = crypto.ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();
            foreach (byte num in bytes)
            {
                sb.AppendFormat("{0:x2}", num);
            }
            return sb.ToString();
        }
        /// <summary>
        /// 鐢熸垚浠ユ椂闂翠负鍩哄噯鐨勫敮涓€鍊?
        /// </summary>
        /// <returns></returns>
        public static string GenerateTimeIdentify()
        {
            return System.DateTime.Now.ToString("yyymmddhhmmss") + System.DateTime.Now.Millisecond.ToString();
        }
        /// <summary>
        /// 鐢熸垚浠ユ椂闂?鑷畾涔変负鍩哄噯鐨勫敮涓€鍊?
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GenerateFileName(string id)
        {
            if (id == "")
                id = "000000";
            else
            {
                string strTemp = id;
                for (int i = 0; i < (6 - strTemp.Length); i++)
                {
                    id = "0" + id;
                }
            }
            return GenerateTimeIdentify() + id;
        }
        /// <summary>
        /// 鐢熸垚鎸囧畾闀垮害鐨勯殢鏈哄瓧绗︿覆 
        /// </summary>
        /// <param name="codeCount">鐢熸垚闀垮害</param>
        /// <returns></returns>
        public static string GenerateRandom(int codeCount)
        {
            int number;
            char code;
            string checkCode = string.Empty;

            System.Random random = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                number = random.Next();
                if (number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));
                checkCode += code.ToString();
            }
            return checkCode;
        }
    }
}

